package
{
	import away3d.core.scene.MouseEvent3D;
	import away3d.core.scene.View3D;
	import away3d.loaders.Collada;
	import away3d.loaders.Object3DLoader;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	
	public class DragTest extends Sprite
	{
		private var loader:Object3DLoader;
		private var view:View3D;
		
		private var lastx :Number = 0;//last mouse sceneX
		private var lasty :Number = 0;
		private var down:Boolean = false;//
		
		public function DragTest()
		{
			stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
			// create a 3D-viewport
			view = new View3D()
			addChild(view);
			
			// start mesh loading
			loader = Collada.load("apple cottage/apple cottage.dae")

			loader.handle.scale(0.1);			
			loader.moveBackward(150000);
			// add the loader object to the scene
			view.scene.addChild(loader);
			
			view.x = stage.stageWidth/2;
            view.y = stage.stageHeight/2;
            
            view.camera.lookAt(loader.position);
            
			view.scene.addOnMouseDown(press); 
			view.scene.addOnMouseMove(mv);
			view.scene.addOnMouseUp(up);
			
			addEventListener(Event.ENTER_FRAME,ENTER_FRAME);
		}

		
		public function press(e:MouseEvent3D):void {
			down = true;
			lastx = e.sceneX;
			lasty = e.sceneY;
		}
	
		public function mv(e:MouseEvent3D):void{
			if(down){
	//			loader.handle.x = e.sceneX;
	//			loader.handle.y = e.sceneY;
				var diffx:Number = e.sceneX - lastx;
				var diffy:Number = e.sceneY - lasty;
				var speed:Number = 0.13;
				
				loader.handle.moveRight(diffx*speed);
//				loader.handle.moveUp(diffy*speed);
				loader.handle.moveBackward(diffy*speed);
			}
		}
		
		public function up(e:MouseEvent3D):void{
			down = false;
		}
		
		public function ENTER_FRAME(event:Event):void {
				// rerender viewport on each frame
				view.render();
		}
		
		
	}
}